package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.bean.User;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.PopListSelect;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by congdy on 17/3/4.
 * 更新个人信息（三围，衣服尺码，标签）
 */

public class AcitivityUpdateUserInfo2 extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.txt_1)
    TextView txt1;
    @BindView(R.id.txt_content)
    TextView txtContent;
    @BindView(R.id.txt_1_r)
    TextView txt1R;
    @BindView(R.id.ll_1)
    LinearLayout ll1;
    @BindView(R.id.et_content1)
    EditText etContent1;
    @BindView(R.id.et_content2)
    EditText etContent2;
    @BindView(R.id.et_content3)
    EditText etContent3;
    @BindView(R.id.txt_2)
    TextView txt2;
    @BindView(R.id.et_content)
    TextView etContent;
    @BindView(R.id.txt_2_r)
    TextView txt2R;
    @BindView(R.id.ll_2)
    LinearLayout ll2;
    @BindView(R.id.ll_pop2)
    LinearLayout ll_pop2;
    private PopListSelect pop;
    public static int updateType;
    private List<Object> selects = new ArrayList<>();

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_update_info_2);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "");
        titleview.setRight("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkedData()) {
                    toUpdateData();
                }
            }
        }, R.color.white);
    }

    private void toUpdateData() {
        MyHashMap map = new MyHashMap();
        setData(map);
        showLoadingDialog();
        UserInfoUtils.toUpdateData(AcitivityUpdateUserInfo2.this, map, new UserInfoUtils.OnUpdateUserInfoBackListener() {
            @Override
            public void back(boolean issuccess, String msg) {
                dissmissLoadingDialog();
                if (issuccess) {
                    if (updateType == ActivityUserInfo.TYPE_CLOTH_SIZE) {
                        UserInfoUtils.updateUserInfoCache(updateType, txtContent.getText().toString().trim());
                    } else {
                        LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
                        if (loginUser != null) {
                            User user = loginUser.getUSER();
                            if (user != null) {
                                user.setBust(etContent1.getText().toString().trim());
                                user.setWaist(etContent2.getText().toString().trim());
                                user.setHips(etContent3.getText().toString().trim());
                                user.setCupSize(etContent.getText().toString().trim());
                            }
                        }
                        UserInfoUtils.updateUserInfoCache(loginUser);
                    }
                    finish();
                    ToastUtil.getInstance().showToast(AcitivityUpdateUserInfo2.this, "保存成功");
                }
            }
        });
    }

    /**
     * * 参数：token,headImg,nickName,sex(1男,2女)
     * ,region(广东省-深圳市),
     * birthDay(1990-05-20),
     * age(26),height(170),
     * weight(70),
     * shoeSize(鞋码42),
     * clothingSize(衣码由基础数据来),
     * bust(胸围),
     * waist(腰围),
     * cupSize(罩杯)
     *
     * @param map
     */
    private void setData(MyHashMap map) {
        switch (updateType) {
            case ActivityUserInfo.TYPE_CLOTH_SIZE:
                map.put("clothingSize", txtContent.getText().toString().trim());
                break;
            case ActivityUserInfo.TYPE_BWH:
                map.put("bust", etContent1.getText().toString().trim());
                map.put("waist", etContent2.getText().toString().trim());
                map.put("hips", etContent3.getText().toString().trim());
                map.put("cupSize", etContent.getText().toString().trim());
                break;
        }
    }

    private boolean checkedData() {
        switch (updateType) {
            case ActivityUserInfo.TYPE_CLOTH_SIZE:
                if (TextUtils.isEmpty(txtContent.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(AcitivityUpdateUserInfo2.this, "请选择您的衣服尺码");
                    return false;
                }
                break;
            case ActivityUserInfo.TYPE_BWH:
                if (TextUtils.isEmpty(etContent1.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(AcitivityUpdateUserInfo2.this, "请输入您的胸围");
                    return false;
                }
                if (TextUtils.isEmpty(etContent2.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(AcitivityUpdateUserInfo2.this, "请输入您的腰围");
                    return false;
                }
                if (TextUtils.isEmpty(etContent3.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(AcitivityUpdateUserInfo2.this, "请输入您的臀围");
                    return false;
                }
                if (TextUtils.isEmpty(etContent.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(AcitivityUpdateUserInfo2.this, "请选择您的罩杯");
                    return false;
                }
                break;
        }
        return true;
    }

    @Override
    public void initData() {
        switch (updateType) {
            case ActivityUserInfo.TYPE_CLOTH_SIZE:
                titleview.setTitle("尺寸");
                txt1.setText("尺码");
                ll2.setVisibility(View.GONE);
                ll1.setVisibility(View.VISIBLE);
                break;
            case ActivityUserInfo.TYPE_BWH:
                titleview.setTitle("三围");
                txt2.setText("罩杯");
                ll2.setVisibility(View.VISIBLE);
                ll1.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @OnClick({R.id.ll_1, R.id.ll_2})
    public void onClick(View view) {
        hideSoftKeyboard();
        View showview = null;
        switch (view.getId()) {
            case R.id.ll_1:
                showview = txtContent;
                break;
            case R.id.ll_2:
                showview = etContent;
                break;
        }
        if (pop == null) {
            pop = new PopListSelect(AcitivityUpdateUserInfo2.this, selects, showview.getMeasuredWidth() + showview.getMeasuredWidth(), new PopListSelect.OnSelectClicListener() {
                @Override
                public void onClick(Object str) {
                    switch (updateType) {
                        case ActivityUserInfo.TYPE_CLOTH_SIZE:
                            txtContent.setText((String)str);
                            break;
                        case ActivityUserInfo.TYPE_BWH:
                            etContent.setText((String)str);
                            break;
                    }
                }
            });
        }
        if (selects.size() == 0) {
            String[] selectstrs = new String[0];
            switch (view.getId()) {
                case R.id.ll_1:
                    selectstrs = getResources().getStringArray(R.array.chothsizes);
                    break;
                case R.id.ll_2:
                    selectstrs = getResources().getStringArray(R.array.bwhsizes);
                    break;
            }
            for (int i = 0; i < selectstrs.length; i++) {
                selects.add(selectstrs[i]);
            }
            pop.setData(selects);
            pop.showAsDropDown(showview);
            return;
        }
        pop.setData(selects);
        pop.showAsDropDown(showview);
    }
}
